//
//  NSDictionary+XM.m
//  XmDemo1
//
//  Created by 徐盟 on 2023/3/31.
//

#import "NSDictionary+XM.h"

@implementation NSDictionary (XM)

- (NSMutableDictionary *) xm_keyValues
{
    return [self xm_keyValuesWithKeys:nil ignoredKeys: nil];
}
- (NSMutableDictionary *) xm_keyValuesWithKeys:(NSArray *)keys{
    return [self xm_keyValuesWithKeys:keys ignoredKeys: nil];
}
- (NSMutableDictionary *) xm_keyValuesWithIgnoreKeys:(NSArray *)keys{
    return [self xm_keyValuesWithKeys: nil ignoredKeys:keys];
}
- (NSMutableDictionary *) xm_keyValuesWithKeys:(nullable NSArray *)keys ignoredKeys: (nullable NSArray *) ignoredKeys
{
    NSMutableDictionary * retDict = [NSMutableDictionary dictionary];
    
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        BOOL isHave = NO;
        BOOL isIgnore = NO;
        if (keys && [keys containsObject:key]) {
            isHave = YES;
        }
        if (ignoredKeys == nil) {
            isIgnore = NO;
        }else if ( [ignoredKeys containsObject:key]) {
            isIgnore = YES;
        }else {
            isIgnore = NO;
        }
        
        if (keys == nil) {
            isHave = YES;
        }else{
            if ([keys containsObject:key]) {
                isHave = YES;
            }else{
                isHave = NO;
            }
        }
        
        if (isIgnore) {
            isHave = NO;
        }
        
        if (isHave) {
            [retDict setObject:obj forKey:key];
        }
        
    }];
    
    return  retDict;
}


@end
